//
//  ActivationCardViewController.m
//  MyWallet
//
//  Created by 小肥羊 on 15/7/16.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "ActivationCardViewController.h"

@interface ActivationCardViewController ()

@end

@implementation ActivationCardViewController

#pragma mark - Set
- (void)setActivationDate:(NSDate *)activationDate
{
    _activationDate = activationDate;
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSString *locationString=[dateformatter stringFromDate:_activationDate];
    [self.changeDateButton setTitle:locationString forState:UIControlStateNormal];
}

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI
{
    [self.cardView.layer setCornerRadius:kNomalRadius];
    [self.cardView.layer setMasksToBounds:YES];
    self.selectDateView = [[SelectDateView alloc] initWithDelegate:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.activationDate = [NSDate date];
    [self.changeDateButton.layer setCornerRadius:kNomalRadius];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.cardNameText becomeFirstResponder];
}

#pragma mark - 切换来源
- (IBAction)changeUsefor:(UISwitch *)sender {
    if (sender.isOn) {
        [self.activationforLabel setText:@"初始"];
    } else {
        [self.activationforLabel setText:@"钱包"];
    }
}

#pragma mark - 完成
- (IBAction)finishActivationCard:(UIBarButtonItem *)sender {
    NSString *cardIndex = [self.receiveDict objectForKey:@"cardIndex"];
    if ([self.activationforLabel.text isEqualToString:@"初始"]) {
        [[TotalAssets sharedManager] activationCard:cardIndex CardName:self.cardNameText.text Money:[self.cardMoneyText.text floatValue] RecordDate:self.activationDate Initialization:YES];
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        if ([[TotalAssets sharedManager] activationCard:cardIndex CardName:self.cardNameText.text Money:[self.cardMoneyText.text floatValue] RecordDate:self.activationDate Initialization:NO]) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"穷逼" message:@"没那么多钱啊" delegate:nil cancelButtonTitle:@"好吧" otherButtonTitles:nil];
            [alter show];
        }
    }
}

#pragma mark - 编辑结束
- (IBAction)finishEditTextFiled:(UITextField *)sender {
    if ([sender isEqual:self.cardNameText]) {
        [self.cardMoneyText becomeFirstResponder];
    }
}

#pragma mark - 点击背景
- (IBAction)touchBackView:(UITapGestureRecognizer *)sender {
    [self.view endEditing:YES];
}

#pragma mark - 修改时间
- (IBAction)changeDate:(UIButton *)sender {
    [self.view endEditing:YES];
    [ActionView showView:self.selectDateView direction:PopViewDirectionStyleCenter];
}

#pragma mark - 时间选择代理
- (void)selectDate:(SelectDateView *)picker
{
    self.activationDate = picker.datePicker.date;
    [[NSNotificationCenter defaultCenter] postNotificationName:CLOESNOTICE object:picker];
}
- (void)cancelSelect:(SelectDateView *)picker
{
    [[NSNotificationCenter defaultCenter] postNotificationName:CLOESNOTICE object:picker];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
